<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>RVM : Class and Code Management</title>
	    <link rel="stylesheet" href="styles/site.css" type="text/css" />
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">	    
    </head>

    <body>
	    <table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
		    <tr>
			    <td valign="top" class="pagebody">
				    <div class="pageheader">
					    <span class="pagetitle">
                            RVM : Class and Code Management
                                                    </span>
				    </div>
				    <div class="pagesubheading">
					    This page last changed on Jul 02, 2008 by <font color="#0050B2">dgrove</font>.
				    </div>

				    <p>The runtime maintains a database of Java instances which identifies the currently loaded class and method base. The classloader class base enables the runtime to identify and dynamically load undefined classes as they required during execution. All the classes, methods and compiled code arrays required to enable the runtime to operate are pre-installed in the initial boot image. Other runtime classes and application classes are loaded dynamically as they are needed during execution and have their methods compiled lazily. The runtime can also identify the latest compiled code array (and, on occasions, previously generated versions of compiled code) of any given method via this classbase and recompile it dynamically should it wish to do so.</p>

<p>Lazy method compilation postpones compilation of a dynamically loaded class' methods at load-time, enabling partial loading of the class base to occur. Immediate compilation of all methods would require loading of all classes mentioned in the bytecode in order to verify that they were being used correctly. Immediate compilation of these class' methods would require yet more loading and so on until the whole classbase was installed. Lazy compilation delays this recursive class loading process by postponing compilation of a method until it is first called.</p>

<p>Lazy compilation works by generating a stub for each of a class' methods when the class is loaded. If the method is an instance method this stub is installed in the appropriate TIB slot. If the method is static it is placed in a linker table located in the JTOC (linker table slots are allocated for each static method when a class is dynamically loaded). When the stub is invoked it calls the compiler to compile the method for real and then jumps into the relevant code to complete the call. The compiler ensures that the relevant TIB slot/linker table slot is updated with the new compiled code array. It also handles any race conditions caused by concurrent calls to the dummy method code ensuring that only one caller proceeds with the compilation and other callers wait for the resulting compiled code.</p>

<h2><a name="ClassandCodeManagement-ClassLoading"></a>Class Loading</h2>

<p>Jikes<a href="http://docs.codehaus.org/display/RVM/Trademarks" title="Trademarks">™</a> RVM implements the Java<a href="http://docs.codehaus.org/display/RVM/Trademarks" title="Trademarks">™</a> programming language's dynamic class loading. While a class is being loaded it can be in one of six states. These are:</p>
<ul>
	<li><b>vacant:</b> The RVMClass object for this class has been created and registered and is in the process of being loaded.</li>
	<li><b>loaded:</b> The class's bytecode file has been read and parsed successfully. The modifiers and attributes for the fields have been loaded and the constant pool has been constructed. The class's superclass (if any) and superinterfaces have been loaded as well.</li>
	<li><b>resolved:</b> The superclass and superinterfaces of this class has been resolved. The offsets (whether in the object itself, the JTOC, or the class's TIB) of its fields and methods have been calculated.</li>
	<li><b>instantiated:</b> The superclass has been instantiated and pointers to the compiled methods or lazy compilation stubs have been inserted into the JTOC (for static methods) and the TIB (for virtual methods).</li>
	<li><b>initializing:</b> The superclass has been initialized and the class initializer is being run.</li>
	<li><b>initialized:</b> The superclass has been initialized and the class initializer has been run.</li>
</ul>


<h2><a name="ClassandCodeManagement-CodeManagement"></a>Code Management</h2>

<p>A compiled method body is an array of machine instructions (stored as ints on PowerPC<a href="http://docs.codehaus.org/display/RVM/Trademarks" title="Trademarks">™</a> and bytes on x86-32).  The <em>Jikes RVM Table of Contents</em>(JTOC), stores pointers to static fields and methods. However, pointers for instance fields and instance methods are stored in the receiver class's <a href="Object Model.html#ObjectModel-TIB">TIB</a>. Consequently, the dispatch mechanism differs between static methods and instance methods.</p>

<h4><a name="ClassandCodeManagement-TheJTOC"></a>The JTOC</h4>

<p>The JTOC holds pointers to each of Jikes<a href="http://docs.codehaus.org/display/RVM/Trademarks" title="Trademarks">™</a> RVM's global data structures, as well as literals, numeric constants and references to String constants. The JTOC also contains references to the <a href="Object Model.html#ObjectModel-TIB">TIB</a> for each class in the system. Since these structures can have many types and the JTOC is declared to be an array of ints, Jikes RVM uses a descriptor array, co-indexed with the JTOC, to identify the entries containing references. The JTOC is depicted in the figure below.</p>

<p><img src="download/attachments/74349/jtoc.gif" align="absmiddle" border="0" title="The Jikes RVM Table Of Contents and other objects."/></p>

<h4><a name="ClassandCodeManagement-VirtualMethods"></a>Virtual Methods</h4>

<p>A <a href="Object Model.html#ObjectModel-TIB">TIB</a> contains pointers to the compiled method bodies (executable code) for the virtual methods and other instance methods of its class. Thus, the <a href="Object Model.html#ObjectModel-TIB">TIB</a> serves as Jikes RVM's virtual method table. A virtual method dispatch entails loading the TIB pointer from the object reference, loading the address of the method body at a given offset off the TIB pointer, and making an indirect branch and link to it. A virtual method is dispatched to with the <em>invokevirtual</em> bytecode; other instance methods are invoked by the <em>invokespecial</em> bytecode.</p>

<h4><a name="ClassandCodeManagement-StaticFieldsandMethods"></a>Static Fields and Methods</h4>

<p>Static fields and pointers to static method bodies are stored in the JTOC. Static method dispatch is simpler than virtual dispatch, since a well-known JTOC entry method holds the address of the compiled method body.</p>

<h4><a name="ClassandCodeManagement-InstanceInitializationMethods"></a>Instance Initialization Methods</h4>

<p>Pointers to the bodies of instance initialization methods, <tt>&lt;init&gt;</tt>, are stored in the JTOC. (They are always dispatched to with the <em>invokespecial</em> bytecode.)</p>

<h4><a name="ClassandCodeManagement-LazyMethodCompilation"></a>Lazy Method Compilation</h4>

<p>Method slots in a TIB or the JTOC may hold either a pointer to the compiled code,  or a pointer to the compiled code of the <em>lazy method invocation stub</em>. When invoked, the lazy method invocation stub compiles the method, installs a pointer to the compiled code in the appropriate <a href="Object Model.html#ObjectModel-TIB">TIB</a> or the JTOC slot, then jumps to the start of the compiled code.</p>

<h4><a name="ClassandCodeManagement-InterfaceMethods"></a>Interface Methods</h4>

<p>Regardless of whether or not a virtual method is overridden, virtual method dispatch is still simple since the method will occupy the same <a href="Object Model.html#ObjectModel-TIB">TIB</a> offset its defining class and in every sub-class. However, a method invoked through an <tt>invokeinterface</tt> call rather than an <tt>invokevirtual call</tt>, will not occupy the same <a href="Object Model.html#ObjectModel-TIB">TIB</a> offset in every class that implements its interface.  This complicates dispatch for <tt>invokeinterface</tt>.</p>

<p>The simplest, and least efficient way, of locating an interface method is to search all the virtual method entries in the <a href="Object Model.html#ObjectModel-TIB">TIB</a> finding a match. Instead, Jikes RVM uses an <em>Interface Method Table</em>(IMT) which resembles a virtual method table for interface methods. Any method that could be an interface method has a fixed offset into the IMT just as with the TIB. However, unlike in the TIB, two different methods may share the same offset into the IMT. In this case, a <em>conflict resolution stub</em> is inserted in the IMT. Conflict resolution stubs are custom-generated machine code sequences that test the value of a hidden parameter to dispatch to the desired interface method. For more details, see <tt>InterfaceInvocation</tt>.</p>

				    					    <br/>
                        <div class="tabletitle">
                            <a name="attachments">Attachments:</a>
                        </div>

                        <div class="greybox" align="left">
                                                            <img src="images/icons/bullet_blue.gif" height="8" width="8" alt=""/>
                                <a href="download/attachments/74349/jtoc.gif">jtoc.gif</a> (image/gif)
                                <br/>
                                                    </div>
				    
                    			    </td>
		    </tr>
	    </table>
	    <table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td height="12" background="http://docs.codehaus.org/images/border/border_bottom.gif"><img src="images/border/spacer.gif" width="1" height="1" border="0"/></td>
			</tr>
		    <tr>
			    <td align="center"><font color="grey">Document generated by Confluence on Jun 10, 2009 09:12</font></td>
		    </tr>
	    </table>
    </body>
</html>